﻿using System;
using System.Data;
using System.Transactions;
using System.Data.Common;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Skyworth.CRM.Service.Entity;
using Skyworth.CRM.Service.IDAL;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Skyworth.CRM.DAL
{
    public class CustDreeDAL : SuperDataBase, ICustDreeDAL
    {
        public void Add(CustDree model)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("insert into T_BasicData_CusDree(");
                strSql.Append("FCustId,FDreeName,FNumber,FName,FTel,FMobile)");
                strSql.Append(" values (");
                strSql.Append("@FCustId,@FDreeName,@FNumber,@FName,@FTel,@FMobile)");
                //strSql.Append(";select @@IDENTITY");
                DbCommand cmd = DB.GetSqlStringCommand(strSql.ToString());
                DB.AddInParameter(cmd, "@FCustId", DbType.Int32, model.FCustId);
                DB.AddInParameter(cmd, "@FDreeName", DbType.String, model.FDreeName);
                DB.AddInParameter(cmd, "@FNumber", DbType.Int32, model.FNumber);
                DB.AddInParameter(cmd, "@FName", DbType.String, model.FName);
                DB.AddInParameter(cmd, "@FTel", DbType.String, model.FTel);
                DB.AddInParameter(cmd, "@FMobile", DbType.String, model.FMobile);
                DB.ExecuteNonQuery(cmd);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Update(CustDree model)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("update T_BasicData_CusDree set ");
                strSql.Append("FCustId=@FCustId,");
                strSql.Append("FDreeName=@FDreeName,");
                strSql.Append("FNumber=@FNumber,");
                strSql.Append("FName=@FName,");
                strSql.Append("FTel=@FTel,");
                strSql.Append("FMobile=@FMobile");
                strSql.Append(" where FItemId=@FItemId");
                DbCommand cmd = DB.GetSqlStringCommand(strSql.ToString());
                DB.AddInParameter(cmd, "@FItemId", DbType.Int32, model.FItemId);
                DB.AddInParameter(cmd, "@FCustId", DbType.Int32, model.FCustId);
                DB.AddInParameter(cmd, "@FDreeName", DbType.String, model.FDreeName);
                DB.AddInParameter(cmd, "@FNumber", DbType.Int32, model.FNumber);
                DB.AddInParameter(cmd, "@FName", DbType.String, model.FName);
                DB.AddInParameter(cmd, "@FTel", DbType.String, model.FTel);
                DB.AddInParameter(cmd, "@FMobile", DbType.String, model.FMobile);
                DB.ExecuteNonQuery(cmd);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Delete(int FItemId)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("delete from T_BasicData_CusDree ");
                strSql.Append(" where FItemId=@FItemId");
                DbCommand cmd = DB.GetSqlStringCommand(strSql.ToString());
                DB.AddInParameter(cmd, "@FItemId", DbType.Int32, FItemId);
                DB.ExecuteNonQuery(cmd);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public CustDree GetModel(int fItemId)
        {
            try
            {
                CustDree model = null;
                StringBuilder strSql = new StringBuilder();
                strSql.Append("select  top 1 FItemId,FCustId,FDreeName,FNumber,FName,FTel,FMobile from T_BasicData_CusDree ");
                strSql.Append(" where FItemId=@FItemId");
                DbCommand cmd = DB.GetSqlStringCommand(strSql.ToString());
                DB.AddInParameter(cmd, "@FItemId", DbType.Int32, fItemId);
                using (IDataReader dr = DB.ExecuteReader(cmd))
                {
                    while (dr.Read())
                    {
                        model = new CustDree();
                        if (dr["FItemId"].ToString() != "")
                        {
                            model.FItemId = int.Parse(dr["FItemId"].ToString());
                        }
                        if (dr["FCustId"].ToString() != "")
                        {
                            model.FCustId = int.Parse(dr["FCustId"].ToString());
                        }
                        model.FDreeName = dr["FDreeName"].ToString();
                        if (dr["FNumber"].ToString() != "")
                        {
                            model.FNumber = int.Parse(dr["FNumber"].ToString());
                        }
                        model.FName = dr["FName"].ToString();
                        model.FTel = dr["FTel"].ToString();
                        model.FMobile = dr["FMobile"].ToString();
                    }
                }
                return model;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable Query(string strWhere)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("select FItemId,FCustId,FDreeName,FNumber,FName,FTel,FMobile ");
                strSql.Append(" FROM T_BasicData_CusDree ");
                if (strWhere.Trim() != "")
                {
                    strSql.Append(" where " + strWhere);
                }
                DataSet ds = DB.ExecuteDataSet(CommandType.Text, strSql.ToString());
                return ds.Tables[0];
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
